# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:ios)

platform :ios do
  desc "Push a new beta build to TestFlight"
  lane :beta do
    # sh "flutter build ios --release --no-codesign --obfuscate --split-debug-info=./split-debug-info --dart-define=LICHESS_HOST=https://lichess.org --dart-define=LICHESS_WS_HOST=wss://socket.lichess.org --dart-define=LICHESS_WS_SECRET=#{ENV['WS_SECRET']}"
    if is_ci
      create_keychain(
        name: ENV['MATCH_KEYCHAIN_NAME'],
        password: ENV["MATCH_KEYCHAIN_PASSWORD"],
        default_keychain: true,
        unlock: true,
        timeout: 3600,
        lock_when_sleeps: false
      )
    end

    sync_code_signing(
      type: "appstore",
      readonly: is_ci,
      api_key: ENV['APP_STORE_KEY_JSON'],
      keychain_name: ENV['MATCH_KEYCHAIN_NAME'],
      keychain_password: ENV["MATCH_KEYCHAIN_PASSWORD"],
    )

    build_app(
      scheme: "Runner",
    )

    app_identifier = CredentialsManager::AppfileConfig.try_fetch_value(:app_identifier)
    apple_identifier = ENV['APPLE_IDENTIFIER']
    ENV["DELIVER_ALTOOL_ADDITIONAL_UPLOAD_PARAMETERS"] = "--apple-id #{apple_identifier} --use-old-altool"

    upload_to_testflight(
      app_identifier: app_identifier,
      apple_id: apple_identifier,
      api_key: ENV['APP_STORE_KEY_JSON'],
      skip_waiting_for_build_processing: true
    )
  end
end
